<?php
//::: XMAP Object :::
//
// @Project: GoldenShield
// @version: 1.1
// @author: Kasper B. Jensen

class XMap {
    
    //Classes to load
    private $loadClases = array("object", "scene", "quest", "character");
    
    //Folder attributes
    public $folders = array("mapFolder" => "maps/");
    public $connectionSize = array("W"=>"40", "H"=>"50", "showBorder" => "false");
    public $colObject = "colobject";
    public $stopPlayer = "stopPlayer";
    public $xmapSubClassExt = ".xmap.class.php";
    
    //XMAP constructer
    public function __construct($configFile = null, $xmlMap = null) {
        if(!empty($configFile)) {
            if(is_string($configFile) && file_exists($configFile)) {
                $this->configFile = $configFile;
                $config = simplexml_load_file($configFile);
                    if(is_object($config)) {
                        $this->xmapConfig = $config;
                    } else {
                        $this->error("Culdn't create XML Object!!!");
                    }
            } elseif(is_object($configFile)) {
                $this->xmapConfig = $configFile;
                $this->readConfig();
            } else {
                $this->error("Culdn't find ". $configFile);
            }
        }
        
        if($xmlMap != null) {
            $this->xmlMap = $xmlMap;
        }
    }
    
    //Reads the entire XMAP config XML files, and uses its content
    public function readConfig() {
        if($this->xmapConfig) {
            //Reads the diffrent folders
            $folders = $this->xmapConfig->config->folders->children();
            foreach($folders as $folder) {
                $type = $folder->attributes()->type;
                $src = $folder->attributes()->src;
                $this->folders["$type"] = $src;
            }
            
            //Loads in the defined itemFiles in xmapConfig
            $itemFiles = $this->xmapConfig->config->itemFiles->children();
            if(!empty($itemFiles)) {
                foreach($itemFiles as $file) {
                    $name = $file->attributes()->name;
                    $src = $file->attributes()->src;

                    
                    if(file_exists($src)) {
                        $xml = simplexml_load_file($src);
                        $this->itemFile["$name"] = $xml;
                    }
                }
            }
            
            //Loads the different classes in
            if(!is_subclass_of($this, 'xmap')) {
                foreach ($this->loadClases as $class) {
                        $this->load($class);
                }
            }
            
            //Reads the connections size
            $this->connectionSize["W"] = $this->xmapConfig->config->connectionSize->attributes()->width;
            $this->connectionSize['H'] = $this->xmapConfig->config->connectionSize->attributes()->height;
            $this->connectionSize['showBorder'] = $this->xmapConfig->config->connectionSize->attributes()->showBorder;
            
            //Reads the collisionclass
            $this->colObject = $this->xmapConfig->config->collisionClass->attributes()->class;
        }
    }

    //Loads the given XMAP file
    public function loadMap($map) {
        if(is_string($map) && !empty($map)) {
            $map = $this->folders['mapFolder']. $map. ".xmap.xml";
            if(file_exists($map)) {
                $map = simplexml_load_file($map);
                if(is_object($map)) {
                    $this->xmlMap = $map;
                    $this->readConfig();
                    $this->createMap();
                } else {
                    $this->error("Culdn't create XML Object!!!");
                }   
            } else {
                $this->error("Culdn't find '$map' !");
            }
        }
    }
    
    //Returns a list of maps
    public function listMaps() {
        $maps = scandir($this->folders['mapFolder']);
        $list = array();
        foreach ($maps as $map) {
            if(strstr($map, "xmap.xml")) {
                $list[] = $map;
            }
        }
        
        if(count($list) > 0) {
            return $list;
        } else {
            return false;
        }
    }
    
    //Creates a unique ID when someone calls it
    public function getUniID($pree = null) {
        return uniqid($pree);
    }
    
    //Creates the map
    public function createMap() {
        //Creates the bg
        $this->scene->createBg();
        
        //Creates the user spawn
        $this->scene->createSpawn();
        
        //Creates connections
        $this->scene->createConnections();
        
        //Creates BGSound
        $this->scene->createBgSound();
        
        //Print objects Objects
        if($this->object->createObjects()) {;
            echo "<div id='objects'>";
            foreach($this->object->createObjects() as $object) {
                echo $object;
            }
        
            //Print sound objects
            if($this->object->sound->printSounds("objectSounds", "objectSoundFolder")) {
                echo "<div id='objectSounds'>";
                foreach($this->object->sound->printSounds("objectSounds", "objectSoundFolder") as $soundTag) {
                    echo $soundTag;
                }
                echo "</div>";
            }
                echo "</div>";
            }
        
        //Create Charectors
        $this->character->createCharectors();
        
        //Output the loadMessage
        $this->quest->showLoadingText();
    }
    
    //Writes XMAP error to screen
    public function error($msg) {
        $err = "<span style='color: red; font-size: 20px; z-index: 200; display: block; position: absolute; top: 300px; background-color: #000; text-align: center;'><b>XMAP Object Server Error</b>: ". $msg. "</span>";
        die($err);
    }
    
    //Loads a new class into this class
    public function load($object = null) {
        if($object != null && is_string($object)) {
            require_once $this->folders['xmapSubclasses']. $object. $this->xmapSubClassExt;
            $this->$object = new $object($this->xmapConfig, $this->xmlMap);
        } else {
            return false;
        }
    }
    
    //Gets the folderpath down to a specific node
    public function getFolderPath($startFolder = null, $pathArray = null) {
        if(!empty($startFolder) && is_array($pathArray)) {
            $l = count($pathArray) - 1;
            foreach($pathArray as $i => $folder) {
                if($i < $l) {
                    $path .= $folder. "/";
                }
            }
            $path = $startFolder. "/". $path;
            return $path;
        }
        return false;
    }
    
    //Returns a specific node
    public function findNode($startNode, $node) {
        if(is_object($startNode) && is_array($node)) {
            foreach($node as $item) {
                $startNode = $startNode->$item;
            }
           
            if(is_object($startNode)) {
                return $startNode;
            }
        }
        return false;
    }
}

?>